Repository navigation
Fixes for low hanging performance "bugs" (AI driven) - #358
KristofferC wants to merge 11 commits into
Conversation
|
I wonder if these |
Recursive lexicographic refinement allocated a fresh merge-sort scratch vector for every subgroup. Thread one workspace through the recursion instead. MWE: benchmark/sortperm_recursive.jl (100,000 rows, 6 Float64 columns, cardinality 16; Julia 1.12.6, minimum of 20 samples). Before: 12.874 ms, 2.87 MiB, 34,473 allocations After: 12.340 ms, 1.62 MiB, 11 allocations
Integer-key refinement allocated and filled a new histogram for every subgroup. Keep one histogram alongside the merge-sort scratch vector and resize/fill it in place. MWE: benchmark/sortperm_recursive.jl (100,000 rows, 6 Int columns, cardinality 16; Julia 1.12.6, minimum of 40 samples; baseline is the preceding commit). Before: 2.356 ms, 2.06 MiB, 17,504 allocations After: 2.206 ms, 960.53 KiB, 12 allocations
The generic Tables fallback pushed rows one at a time without reserving component capacity. Apply sizehint! when the iterator reports a length, while retaining the existing row-wise conversion path. MWE: benchmark/append_rows.jl (1,000,000 NamedTuple rows, 4 columns; Julia 1.12.6, minimum of 10 samples). Before: 9.555 ms, 115.14 MiB, 120 allocations After: 8.034 ms, 30.56 MiB, 8 allocations
Shape validation mapped axes over every component before reducing the results. For homogeneous concrete component tuples, validate in a type-stable loop and return early on a mismatch.
MWE: benchmark/wide_constructor.jl (128 Vector{Float64} columns; Julia 1.12.6, minimum BenchmarkTools estimate).
Before: 1.983 us, 1.23 KiB, 3 allocations
After: 56.233 ns, 0 bytes, 0 allocations
Wide homogeneous component tuples already encode their shared array type. Derive the row tuple type directly instead of mapping eltype and splatting a large temporary tuple; retain the fallback for abstract and heterogeneous components.
MWE: benchmark/wide_constructor.jl (inferred 128-column Vector{Float64} StructArray; Julia 1.12.6, minimum BenchmarkTools estimate; baseline is the preceding commit).
Before: 4.244 us, 4.61 KiB, 13 allocations
After: 56.233 ns, 0 bytes, 0 allocations
Recursive Tuple-tail comparison crosses Julia's specialization limit for wide StructArrays and boxes each comparison. Compare homogeneous concrete component tuples with a type-stable loop while retaining recursive dispatch for heterogeneous tuples. MWE: benchmark/group_wide.jl (100,000 rows, 64 Int columns, cardinality 16; Julia 1.12.6, minimum of 10 samples). Before: 365.092 ms, 355.32 MiB, 698,520 allocations After: 728.416 us, 0 bytes, 0 allocations
Scalar getindex already materializes a tuple of component values. Tuple and NamedTuple row types can construct directly from it, avoiding the specialization cliff from a wide vararg splat while preserving their conversions. MWE: benchmark/wide_getindex.jl (128 Float64 columns, 100 rows; Julia 1.12.6, minimum BenchmarkTools estimate). Before: 2.509 us, 4.16 KiB, 131 allocations After: 54.795 ns, 0 bytes, 0 allocations
NamedTuple component collections hit the same wide-tuple specialization limit during axes validation. Above that limit, use the encoded homogeneous value-tuple type to select the allocation-free loop; retain the unrolled path for narrow static arrays and the generic fallback for heterogeneous or abstract fields. MWE: benchmark/wide_constructor.jl (inferred 128-column named StructArray; Julia 1.12.6, minimum BenchmarkTools estimate). Before: 3.869 us, 3.36 KiB, 5 allocations After: 79.459 ns, 0 bytes, 0 allocations
The `Tuple` and `NamedTuple` generated methods of `findconsistentvalue` were identical, so merge them into a single `T<:Tup` method. Name the shared width cutoff `WIDE_TUPLE_LENGTH` instead of repeating `32` in `utils.jl` and `sort.jl`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The loop-based paths only trigger above `WIDE_TUPLE_LENGTH` homogeneous components, which no existing test reached. Add tests for: - construction (`Tuple` and `NamedTuple`): inference, zero allocations, shape-mismatch errors, and heterogeneous fallback - scalar `getindex`: values, eltype conversion, and zero allocations - `roweq` and `GroupPerm` on wide arrays - `sortperm` with nested groups, so the shared scratch and histogram get reused and resized - `append!` from iterators with known and unknown lengths The wide-tuple tests fail on the previous `master` with an inference error and allocations. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The generated `findconsistentvalue` looped over the components, which indexed them dynamically. On Julia 1.14 this put the freshly built wide `NamedTuple` (from `strip_params(T)(c)`) on the heap, about 34 KiB per construction. Unroll the checks with constant field indices instead; this is also faster to construct. - `roweq(::StructArray, i, j)` converted the components with `Tuple(...)`, which allocated for `NamedTuple` components on every Julia version. Take the components as they are; `roweq(::Tup, ...)` now indexes them with `getfield` directly. - Move the `findconsistentvalue` docstring back onto the public function. It was on the private `_findconsistentvalue` helper, which broke the docs build. - Tests: measure allocations behind a function barrier and store whole rows rather than `sum` them, so the checks don't count boxing on Julia 1.10 and 1.11. Also test `roweq` with wide `NamedTuple` components. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
At some point maybe? For now, they are here 🤷 |
|
yes that makes sense in the meanwhile :) Performance improvements are always great, I just don't see why homogeneous vs heterogeneous column types difference is relevant here. It would be super confusing if just changing one column from Int to Float suddenly dropped performance by orders of magnitude... Also, I personally find it harder to review large PRs with multiple basically-unrelated changes, where one needs to approve everything wholesale. For these two reasons, I made a smaller focused #370 covering many of these wide-StructArray improvements (plus more functions, and including heterogeneous cols), but focusing on a single specific solution – not all at once. |
That's fair. I tried to make it so that every commit is small and individually useful, and I didn't want to spam the repo with a bunch of PRs. |
|
I made a few self-contained PRs for individual improvements, and #370 #376 #377 are specifically related to this one (#374 and #375 are unrelated improvements). They focus on one mechanism each, and apply it consistently to everything affected, getting more cases fixed than here. All three – about different wide-column scenarios. All of them look like straightforward improvements, no downsides and no api changes at all. As for other parts of this PR, I think I'd approve them as-is: they are not parts of broader/more general patterns, so there's nothing to generalize really... |
This PR is a set of small commits. Each one makes a large difference to allocations or runtime in a specific case, and each comes with a benchmark script in
benchmark/(the commit message has that commit's own before/after numbers). Note that this PR was mostly generated via AI but is still opened in the hope that it nonetheless is useful.Most of the changes target wide
StructArrays: more thanWIDE_TUPLE_LENGTH = 32columns of the same concrete type. Recursive or splatting tuple code hits Julia's specialization limits at that width, so these paths use a plain loop instead. Narrower and heterogeneous component tuples keep the existing code. The other changes reuse sort scratch space across key groups andsizehint!the columns when appending rows of known length.The last commit adds tests for all of these paths. They fail on
master, with an inference error and allocations on the wide paths.Benchmarks
Before is
master(c2a046f), after is this PR (a2d2d0b). Minimum time from BenchmarkTools, Apple silicon (aarch64 macOS), run with-O3.Julia 1.13.3
Julia 1.12.7